package com.ghc.androidexample.user;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.LoaderManager.LoaderCallbacks;
import android.content.CursorLoader;
import android.content.Loader;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.ContactsContract.Contacts;
import android.support.v4.widget.CursorAdapter;
import android.support.v4.widget.SimpleCursorAdapter;
import android.text.TextUtils;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.ghc.androidexample.R;

public class ContactsActivity extends Activity implements
		LoaderCallbacks<Cursor>, OnItemClickListener {
	private ListView mLvContact;

	@SuppressLint("InlinedApi")
	private static final String[] FROM_COLUMNS = { Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB ? Contacts.DISPLAY_NAME_PRIMARY
			: Contacts.DISPLAY_NAME };
	private static final int[] TO_IDS = { R.id.tvContactName };

	@SuppressLint("InlinedApi")
	private static final String[] PROJECTION = {
			Contacts._ID,
			Contacts.LOOKUP_KEY,
			Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB ? Contacts.DISPLAY_NAME_PRIMARY
					: Contacts.DISPLAY_NAME

	};

	private long mContactId;
	private String mContactKey;
	private Uri mContactUri;
	private SimpleCursorAdapter mCursorAdapter;

	// The column index for the _ID column
	private static final int CONTACT_ID_INDEX = 0;
	// The column index for the LOOKUP_KEY column
	private static final int LOOKUP_KEY_INDEX = 1;

	@SuppressLint("InlinedApi")
	private static final String SELECTION = Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB ? Contacts.DISPLAY_NAME_PRIMARY
			+ " LIKE ?"
			: Contacts.DISPLAY_NAME + " LIKE ?";
	// Defines a variable for the search string
	private String mSearchString;
	// Defines the array to hold values that replace the ?
	private String[] mSelectionArgs = { mSearchString };

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_contacts);

		mLvContact = (ListView) findViewById(R.id.lvContactList);
		mCursorAdapter = new SimpleCursorAdapter(this,
				R.layout.contacts_list_item, null, FROM_COLUMNS, TO_IDS, 0);
		mLvContact.setOnItemClickListener(this);
		mLvContact.setAdapter(mCursorAdapter);
		
		getLoaderManager().initLoader(0, null, this);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.contacts, menu);
		return true;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * android.widget.AdapterView.OnItemClickListener#onItemClick(android.widget
	 * .AdapterView, android.view.View, int, long)
	 */
	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		// Get the Cursor
		Cursor cursor = ((CursorAdapter) parent.getAdapter()).getCursor();
		// Move to the selected contact
		cursor.moveToPosition(position);
		// Get the _ID value
		mContactId = cursor.getLong(CONTACT_ID_INDEX);
		// Get the selected LOOKUP KEY
		mContactKey = cursor.getString(LOOKUP_KEY_INDEX);
		// Create the contact's content Uri
		mContactUri = Contacts.getLookupUri(mContactId, mContactKey);
		
		ContactDetail contactDetail = new ContactDetail(this, mContactUri);
		contactDetail.show();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.LoaderManager.LoaderCallbacks#onCreateLoader(int,
	 * android.os.Bundle)
	 */
	@Override
	public Loader<Cursor> onCreateLoader(int id, Bundle args) {
		/*
		 * Makes search string into pattern and stores it in the selection array
		 */
		// mSelectionArgs[0] = "%" + mSearchString + "%";
		CursorLoader cursorLoader = null;
		if (mSelectionArgs.length == 0 || TextUtils.isEmpty(mSelectionArgs[0])) {
			cursorLoader = new CursorLoader(ContactsActivity.this,
					Contacts.CONTENT_URI, PROJECTION, null, null, null);
		} else {
			cursorLoader = new CursorLoader(ContactsActivity.this,
					Contacts.CONTENT_URI, PROJECTION, SELECTION,
					mSelectionArgs, null);
		}

		return cursorLoader;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * android.app.LoaderManager.LoaderCallbacks#onLoadFinished(android.content
	 * .Loader, java.lang.Object)
	 */
	@Override
	public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
		// Put the result Cursor in the adapter for the ListView
		mCursorAdapter.swapCursor(cursor);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * android.app.LoaderManager.LoaderCallbacks#onLoaderReset(android.content
	 * .Loader)
	 */
	@Override
	public void onLoaderReset(Loader<Cursor> loader) {
		mCursorAdapter.swapCursor(null);
	}

}
